package com.sky.happygos.ui.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.sky.happygos.R;
import com.sky.happygos.YiPush.Msg;
import com.sky.happygos.YiPush.YiPush;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.model.Goods;
import com.sky.happygos.network.TokenPrefs;
import com.sky.happygos.network.TokenPrefs_;
import com.sky.happygos.present.GoodsP;
import com.sky.happygos.present.UserP;
import com.sky.happygos.ui.activity.fragment.GoodsF_;
import com.sky.happygos.ui.activity.fragment.OrderF_;
import com.sky.happygos.ui.activity.fragment.PersonF_;
import com.sky.happygos.ui.activity.fragment.TransactionF_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import static com.sky.happygos.ui.activity.MainA.EXTRA_TAB;

@EActivity(R.layout.activity_main)
public class MainA extends BaseA implements BottomNavigationBar.OnTabSelectedListener {

    public static final int TYPE_Normal = 1;
    public static final int TYPE_Progress = 2;
    public static final int TYPE_BigText = 3;
    public static final int TYPE_Inbox = 4;
    public static final int TYPE_BigPicture = 5;
    public static final int TYPE_Hangup = 6;
    public static final int TYPE_Media = 7;
    public static final int TYPE_Customer = 8;
    private NotificationManager manger;

    public static final String EXTRA_TAB = "EXTRA_TAB";


    @ViewById(R.id.bottomNavigationBar)
    BottomNavigationBar bottomNavigationBar;



    Fragment shopF;
    Fragment transactionF;
    Fragment orderF;
    Fragment personF;
    FragmentManager fragmentManager;
    YiPush yiPush;

    @Pref
    TokenPrefs_ tokenPrefs;
    @Extra(EXTRA_TAB)
    int tabNum;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        String userid = tokenPrefs.userid().get();

        yiPush = YiPush.builder()
                .setPushServer("192.168.1.103",8133)
                .setUserid(userid)
                .setReceiveMessageCallBack(new YiPush.YiPushCallBack() {
                    @Override
                    public void receiveMessage(final Msg msg) {
                        EventBus.getDefault().post(msg);
                    }

                    @Override
                    public void connectCallBack(boolean status) {
                        if(status){
                            Toast.makeText(MainA.this,"Yipush"+"连接成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(MainA.this,"Yipush"+"连接失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .build();

        yiPush.start(getApplicationContext());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void receiveMessage(Msg msg){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("订单提醒");
        builder.setContentText(msg.data);
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setSmallIcon(R.drawable.app_icon);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.app_icon));
        Intent intent = new Intent(this,MainA_.class);
        PendingIntent pIntent = PendingIntent.getActivity(this,1,intent,0);
        builder.setContentIntent(pIntent);
        builder.setFullScreenIntent(pIntent,true);
        builder.setAutoCancel(true);
        Notification notification = builder.build();
        manger.notify(TYPE_Hangup,notification);
    }



    @AfterViews
    public void initViews() {
        shopF = new GoodsF_();
        transactionF = new TransactionF_();
        orderF = new OrderF_();
        personF = new PersonF_();
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,shopF);
        transaction.add(R.id.content,transactionF);
        transaction.add(R.id.content,orderF);
        transaction.add(R.id.content,personF);
        transaction.show(shopF);
        transaction.hide(transactionF);
        transaction.hide(orderF);
        transaction.hide(personF);
        transaction.commit();

//        setScrollableText(lastSelectedPosition);
//
//        numberBadgeItem = new BadgeItem()
//                .setBorderWidth(4)
//                .setBackgroundColorResource(R.color.blue)
//                .setText("" + lastSelectedPosition)
//                .setHideOnSelect(autoHide.isChecked());


//        if (modeFixed.isChecked()) {
            bottomNavigationBar
                    .setMode(BottomNavigationBar.MODE_FIXED);
//        } else if (modeShifting.isChecked()) {
//            bottomNavigationBar
//                    .setMode(BottomNavigationBar.MODE_SHIFTING);

//            bottomNavigationBar
//                    .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
            bottomNavigationBar
                    .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setTabSelectedListener(this);


        BadgeItem numberBadgeItem = new BadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.main)
                .setText("5")
                .setHideOnSelect(true);


        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.tab_shop_fill, "主页").setInactiveIconResource(R.drawable.tab_shop).setActiveColorResource(R.color.gallery_white))
                .addItem(new BottomNavigationItem(R.drawable.tab_trans_fill, "交易").setInactiveIconResource(R.drawable.tab_trans).setActiveColorResource(R.color.gallery_white).setBadgeItem(numberBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.tab_order_fill, "订单").setInactiveIconResource(R.drawable.tab_order).setActiveColorResource(R.color.gallery_white))
                .addItem(new BottomNavigationItem(R.drawable.tab_person_fill, "个人").setInactiveIconResource(R.drawable.tab_person).setActiveColorResource(R.color.gallery_white))
                .setFirstSelectedPosition(tabNum)
                .initialise();
        bottomNavigationBar.selectTab(tabNum);
    }


    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        int selectTab = intent.getIntExtra(EXTRA_TAB,0); 
    }

    @Override
    public void onTabSelected(int position) {
        switch (position){
            case 0:{
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.show(shopF);
                transaction.hide(transactionF);
                transaction.hide(orderF);
                transaction.hide(personF);
                transaction.commit();
                break;
            }
            case 1:{
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.show(transactionF);
                transaction.hide(shopF);
                transaction.hide(orderF);
                transaction.hide(personF);
                transaction.commit();
                break;
            }
            case 2:{
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.show(orderF);
                transaction.hide(transactionF);
                transaction.hide(shopF);
                transaction.hide(personF);
                transaction.commit();
                break;
            }
            case 3:{
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.show(personF);
                transaction.hide(transactionF);
                transaction.hide(orderF);
                transaction.hide(shopF);
                transaction.commit();
                break;
            }

        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

}
